package cn.mengfly.testnrz.netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.embedded.EmbeddedChannel;

public class TestPipline {
    public static class SimpleInHandlerA extends ChannelInboundHandlerAdapter {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            System.out.println("in A called"+ msg.hashCode() + msg);
            ctx.fireChannelRead(msg);
        }
    }

    public static class SimpleInHandlerB extends ChannelInboundHandlerAdapter {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            System.out.println("in B called "+ msg.hashCode()+ msg);
            super.channelRead(ctx, "123");
        }
    }

    public static class SimpleInHandlerC extends ChannelInboundHandlerAdapter {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            System.out.println("in C called" + msg.hashCode()+ msg);
            super.channelRead(ctx, msg);
        }
    }

    public static class SimpleOutHandlerA extends ChannelOutboundHandlerAdapter {
        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            System.out.println("out A called"+ msg.hashCode()+ msg);
            super.write(ctx, msg, promise);
        }
    }

    public static class SimpleOutHandlerB extends ChannelOutboundHandlerAdapter {
        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            System.out.println("out B called"+ msg.hashCode()+ msg);
            super.write(ctx, msg, promise);
        }
    }

    public static class SimpleOutHandlerC extends ChannelOutboundHandlerAdapter {
        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            System.out.println("out C called "+ msg.hashCode()+ msg);
            super.write(ctx, msg, promise);
        }
    }

    public static void main(String[] args) {
        ChannelInitializer<EmbeddedChannel> i = new ChannelInitializer<EmbeddedChannel>() {
            @Override
            protected void initChannel(EmbeddedChannel embeddedChannel) throws Exception {
                embeddedChannel.pipeline().addLast(new SimpleInHandlerA());
                embeddedChannel.pipeline().addLast(new SimpleOutHandlerA());
                embeddedChannel.pipeline().addLast(new SimpleInHandlerB());
                embeddedChannel.pipeline().addLast(new SimpleOutHandlerB());
                embeddedChannel.pipeline().addLast(new SimpleInHandlerC());
                embeddedChannel.pipeline().addLast(new SimpleOutHandlerC());
            }
        };
        EmbeddedChannel channel = new EmbeddedChannel(i);
        ByteBuf buf = Unpooled.buffer();
        buf.writeInt(1);
        channel.writeInbound(buf);
        channel.flush();
        channel.writeOutbound(buf);
        channel.disconnect();
    }
}
